#include "exportdialog.h"
#include "ui_exportdialog.h"
#include <QDebug>
#include <iostream>
#include <fstream>
#include <string>
#include <QTimer>
#include "msginf.h"

ExportDialog::ExportDialog(CSql* sql,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ExportDialog),
    m_Sql(sql)
{
    ui->setupUi(this);
    this->setWindowTitle(_("ExportData"));
    this->setFixedSize(303,175);

    ui->dateEdit->setDate(QDate(QDateTime::currentDateTime().date().year(),
                                QDateTime::currentDateTime().date().month(),
                                QDateTime::currentDateTime().date().day()));

    ui->label->setText(_("ExportDate"));
    ui->label_2->setText(_("Project"));
    ui->pBExport->setText(_("Export"));
    ui->pBCancel->setText((_("Cancel")));
    QStringList items;
    NODE nd;
    std::string value,vlayer;;
    items << tr("Spring") << tr("Summer") << tr("Fall") << tr("Winter");
    items.clear();


    RECORDSET rd = m_Sql->RecordSet("select * from fsapp");

    if(rd.size()>0){
        for(RECORDSET::iterator it = rd.begin();it!=rd.end();it++){
        //for(int i = 0;i<rd.size();i++){
            nd =  *it;
            value = nd["value"];
            items << QString::fromLocal8Bit(value.c_str());
        }
    }
    ui->comboBox->addItems(items);

    ui->progressBar->setValue(0);
    connect(ui->pBExport,SIGNAL(clicked()),this,SLOT(myExport()));
}
/*
classid
point
gap
flush
state
fsapp
dt
tim
*/
void ExportDialog::myExport()
{
    qDebug() << __FUNCTION__;
    ui->progressBar->setValue(0);
    QTimer::singleShot(0,this,SLOT(ExportData()));

}
void ExportDialog::ExportData()
{
    NODE nd;
    std::string value,vlayer;;
    QString sql;
    QString dt;
    dt = QString("%1").arg(ui->dateEdit->date().year(), 4, 10, QChar('0'))  +
         QString("%1").arg(ui->dateEdit->date().month(), 2, 10, QChar('0')) +
         QString("%1").arg(ui->dateEdit->date().day(), 2, 10, QChar('0'));

    sql = QString("select * from fsdata where fsapp = '%1' and dt = '%2'").arg(ui->comboBox->currentText(),dt);
    qDebug() << sql;
    RECORDSET rd = m_Sql->RecordSet(sql.toStdString());
    int i = 0;
    if(rd.size()>0){
        ui->progressBar->setRange(0,rd.size());
        for(RECORDSET::iterator it = rd.begin();it!=rd.end();it++){
        //for(int i = 0;i<rd.size();i++){
            nd =  *it;
            value.append(nd["fsapp"]);value.append(",");
            value.append(nd["point"]);value.append(",");
            value.append(nd["gap"]);value.append(",");
            value.append(nd["flush"]);value.append(",");
            value.append(nd["state"]);value.append(",");
            value.append(nd["dt"]);value.append(",");
            value.append(nd["classid"]);value.append("\n");
            //qDebug() << nd["fsapp"].c_str() << nd["point"].c_str << nd["gap"].c_str << nd["flush"].c_str << nd["state"].c_str << nd["dt"].c_str;
            ui->progressBar->setValue(++i);
        }
        qDebug() << value.c_str();

        //QApplication::applicationDirPath()

        std::string filename =QApplication::applicationDirPath().toStdString() +"/Export/" +
                ui->comboBox->currentText().toStdString()+ "-" + dt.toStdString()+".csv";
        qDebug() << filename.c_str();
        {
                std::ofstream ostrm(filename,ios_base::out|ios_base::trunc);

                ostrm.write(value.c_str(), value.length()); // 二进制输出
                //ostrm << 123 << "abc" << '\n';                      // 文本输出
        }
        value.clear();
        MSGINF::MsgShowOk(this,_("ExportData"),_("Export OK")+"\n"+QString(filename.c_str()));
    }
    else
        MSGINF::MsgShowOk(this,_("ExportData"),_("Nothing Data Export"));


}
ExportDialog::~ExportDialog()
{
    delete ui;
}

void ExportDialog::on_pBCancel_clicked()
{
    close();
}
